<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/11/1
 * Time: 下午6:45
 */

namespace app\common\command;

use app\common\model\CardConsume;
use app\common\model\CardUserGet;
use app\common\model\CardUserUpdate;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\Mstatis;
use app\common\model\Paylog;
use app\common\model\ProfitList;
use app\common\model\Statis;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Queue;

class Fenzhang extends Command
{
    protected function configure()
    {
        $this->setName('fenzhang')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->fenzhang();

        $output->writeln("分账处理成功:");
    }

    public function fenzhang()
    {
        $Merchant = new Merchant();
        $start_time = date('H') . ":00:00";
        $end_time = (date('H') + 1) . ":00:00";
        $map['is_profit']="Y";
        $map['profit_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $machinfo = $Merchant->where($map)->column('id');//列出符合当前时间状态的用户
        $mid = implode($machinfo, ",");
        $ProfitList = new ProfitList();
        $map2['mid'] = array('in', $mid);
        $map2['status'] = array('neq',"Y");

        $map2['create_time'] = array(
            array('egt', (strtotime(date('Y-m-d H') . ":00:00") - 86400)),
            array('elt', strtotime(date('Y-m-d H') . ":00:00"))
        );
        $list = $ProfitList->where($map2)->select();
        foreach($list as $k=>$v){
            $this->task_biz_fenzhang($v);
        }

    }


    private function task_biz_fenzhang($data = ''){

        // 1.当前任务将由哪个类来负责处理。
        //   当轮到该任务时，系统将生成一个该类的实例，并调用其 fire 方法
        $jobHandlerClassName  = 'app\merchant\job\Fenzhang';
        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName  	  = "task_biz_fenzhang";
        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
        $jobData = $data ;
        // 4.将该任务推送到消息队列，等待对应的消费者去执行
        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
        if( $isPushed !== false ){
            return 'success';
        }else{
            return 'error';
        }
    }

}
